package com.mxpio.erp.mes.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.MesEnums;
import com.mxpio.erp.common.mes.entity.ManufactureWorkOrder;
import com.mxpio.erp.common.mes.entity.ManufactureWorkOrderTask;
import com.mxpio.erp.common.mes.entity.MesScanLog;
import com.mxpio.erp.common.mes.service.ManufactureWorkOrderTaskService;
import com.mxpio.erp.common.technology.entity.WorkUnit;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ManufactureWorkOrderTaskServiceImpl extends BaseServiceImpl<ManufactureWorkOrderTask> implements ManufactureWorkOrderTaskService {

    @Override
    @Transactional
    public Result<?> dispatch(ManufactureWorkOrderTask task) {//派单，分配任务到具体机台
        ManufactureWorkOrderTask dbTask = this.getById(ManufactureWorkOrderTask.class,task.getTaskCode());
        if (!StringUtils.equals(CommonEnums.YesNo.YES.getCode(), dbTask.getDispatchAllowed())){
            return Result.error("任务不允许派工！");
        }
        if (!StringUtils.equals(MesEnums.DispatchMode.ASSIGN.getCode(), dbTask.getDispatchMode())){
            return Result.error("任务非派单模式，不能派工！");
        }
        if (!StringUtils.equals(MesEnums.WorkOrderTaskStatus.NEW.getCode(), dbTask.getTaskStatus())){
            return Result.error("非开立状态，任务不允许派工操作！");
        }
        String workUnitCode = task.getWorkUnitCode();
        if (StringUtils.isBlank(workUnitCode)){
            return Result.error("请选择作业单元！");
        }
        WorkUnit workUnit = JpaUtil.getOne(WorkUnit.class, workUnitCode);
        if (workUnit == null){
            return Result.error("作业单元不存在！");
        }
        if (!StringUtils.equals(task.getProcCode(), workUnit.getProcessCode())){
            return Result.error("任务工序和作业单元工序不一致！");
        }
        if (!StringUtils.equals(task.getWorkCenterCode(), workUnit.getWorkCenterCode())){
            return Result.error("任务工作中心和作业单元工作中心不一致！");
        }
        dbTask.setWorkUnitCode(task.getWorkUnitCode());
        dbTask.setWorkUnitName(workUnit.getWorkUnitName());
        dbTask.setDispatchTime(new Date());
        dbTask.setDispatchUserId(SecurityUtils.getLoginUsername());
        dbTask.setDispatchAllowed(CommonEnums.YesNo.NO.getCode());
        dbTask.setTaskStatus(MesEnums.WorkOrderTaskStatus.READY.getCode());//待作业
        this.update(dbTask);
        /*MesScanLog mesScanLog = new MesScanLog();
        mesScanLog.setBizNo(dbTask.getBizNo());
        mesScanLog.setBatchCode(dbTask.getBatchCode());
        mesScanLog.setTaskCode(dbTask.getTaskCode());
        mesScanLog.setOperationType("工单任务派工");
        JpaUtil.save(mesScanLog);*/

        return Result.OK("派单成功",dbTask);
    }

    @Override
    @Transactional//开工
    public Result<?> start(String taskCode) {
        ManufactureWorkOrderTask dbTask = this.getById(ManufactureWorkOrderTask.class, taskCode);
        if (!StringUtils.equals(MesEnums.WorkOrderTaskStatus.READY.getCode(), dbTask.getTaskStatus())){
            return Result.error("任务仅待作业状态下才允许开工操作！");
        }
        String batchCode = dbTask.getBatchCode();
        ManufactureWorkOrder workOrder = JpaUtil.getOne(ManufactureWorkOrder.class, batchCode);
        if (!(StringUtils.equals(MesEnums.WorkOrderStatus.SEND.getCode(), workOrder.getBatchStatus())
                ||StringUtils.equals(MesEnums.WorkOrderStatus.WORKING.getCode(), workOrder.getBatchStatus()))){
            return Result.error("工单仅“下达”和“作业中”时允许任务开工操作！");
        }
        if (dbTask.getProcOrder()!=workOrder.getNextProcOrder()){
            return Result.error("工单未执行到当前工序任务，不允许开工操作！");
        }

        dbTask.setReStartDate(new Date());
        dbTask.setTaskStatus(MesEnums.WorkOrderTaskStatus.WORKING.getCode());
        this.update(dbTask);
        if (dbTask.getTaskIndex()==1){//
            workOrder.setReStartDate(dbTask.getReStartDate());
            if (StringUtils.equals(MesEnums.WorkOrderStatus.SEND.getCode(), workOrder.getBatchStatus())){
                workOrder.setBatchStatus(MesEnums.WorkOrderStatus.WORKING.getCode());
            }
            JpaUtil.update(workOrder);//更新实际开工时间和工单状态
        }


        MesScanLog mesScanLog = new MesScanLog();
        mesScanLog.setBizNo(dbTask.getBizNo());
        mesScanLog.setBatchCode(dbTask.getBatchCode());
        mesScanLog.setTaskCode(dbTask.getTaskCode());
        mesScanLog.setOperationType("工单任务开工");
        JpaUtil.save(mesScanLog);
        return Result.OK();
    }
}
